package com.bci.pwtz.service.impl;

import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.VzdyMapper;
import com.bci.pwtz.mysql.model.Vzdy;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.VzdyService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class VzdyServiceImpl implements VzdyService
{

    
    private VzdyMapper objMapper;
    
    private CompanyService companyService;
    
    /**
     * @return the objMapper
     */
    public VzdyMapper getObjMapper()
    {
        return objMapper;
    }

    /**
     * @param objMapper the objMapper to set
     */
    public void setObjMapper(VzdyMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    /**
     * @return the companyService
     */
    public CompanyService getCompanyService()
    {
        return companyService;
    }

    /**
     * @param companyService the companyService to set
     */
    public void setCompanyService(CompanyService companyService)
    {
        this.companyService = companyService;
    }

    private static final Logger _logger = Logger.getLogger(VzdyServiceImpl.class);
    
    @Override
    public List<Vzdy> findList(Long companyId,int type,String levelname) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("findVzdy:|companyId=" + companyId);
        }
        List<Vzdy> list = null;
        String companyidStr = "";
        String[] companyids=null;
        if(type==1){
            companyidStr = companyService.findChildrensId(companyId,null);
            if(companyidStr!=null && companyidStr.length()>0){
                companyidStr= companyidStr.substring(0,companyidStr.length()-1);
                companyids =companyidStr.split(",");
            }
        }
        try
        {
            list = objMapper.selectList(companyId,companyids,levelname);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findVzdy:result list=" + list.toString());
        }

        return list;
    }
    
    @Override
    public List<Vzdy> findListByUserId(Long companyId,int type,Long operatorId,String beginTime,String endTime,String levelname) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("findVzdy:|companyId=" + companyId+",operatorId="+operatorId+",beginTime="+beginTime+",endTime="+endTime);
        }
        List<Vzdy> list = null;
        String companyidStr = "";
        String[] companyids=null;
        if(type==1){
            companyidStr = companyService.findChildrensId(companyId,null);
            if(companyidStr!=null && companyidStr.length()>0){
                companyidStr= companyidStr.substring(0,companyidStr.length()-1);
                companyids =companyidStr.split(",");
            }
        }
        try
        {
            list = objMapper.selectListByUserId(companyId, companyids, operatorId, beginTime, endTime,levelname);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findVzdy:result list=" + list.toString());
        }

        return list;
    }
}
